# 37aa5dc36d88444786b6b543845963ba
# 37aa5dc36d88444786b6b543845963ba20 缩略图
# 37aa5dc36d88444786b6b543845963ba21 预览图
# 37aa5dc36d88444786b6b543845963ba29 高清图

import requests
import os
import time

root = "./images"
if not os.path.exists(root):
    os.mkdir(root)
thumb_root = os.path.join(root,'thumb_images')
if not os.path.exists(thumb_root):
    os.mkdir(thumb_root)
preview_root = os.path.join(root,'preview_images')
if not os.path.exists(preview_root):
    os.mkdir(preview_root)
big_root = os.path.join(root,'big_images')
if not os.path.exists(big_root):
    os.mkdir(big_root)


for page in range(1,10):
    print(f'正在爬取的页面是{page}')
    url = "https://api.zzzmh.cn/v2/bz/v3/getData"
    json_data = {
        "size": 24,
        "current": page,
        "sort": 0,
        "category": 0,
        "resolution": 0,
        "color": 0,
        "categoryId": 0,
        "ratio": 0
    }
    res = requests.post(url=url,json=json_data)
    print(res.json()['data']['list'])
    if res.status_code == 200:
        for item in res.json()['data']['list']:
            i = item['i']
            t = item['t']
            print(f'正在爬取的图片是{i}')
            # 缩略图
            thumb_url = f'https://api.zzzmh.cn/v2/bz/v3/getUrl/{i}{t*10}'
            thumb_res = requests.get(thumb_url)
            with open (f"{thumb_root}/thumb_{i}.jpg",'wb') as f:
                f.write(thumb_res.content)
            # 预览图
            preview_url = f'https://api.zzzmh.cn/v2/bz/v3/getUrl/{i}{t * 10+1}'
            preview_res = requests.get(preview_url)
            with open(f"{preview_root}/preview_{i}.jpg", 'wb') as f:
                f.write(preview_res.content)
            # 高清图
            big_url = f'https://api.zzzmh.cn/v2/bz/v3/getUrl/{i}{t * 10 + 9}'
            big_res = requests.get(big_url)
            with open(f"{big_root}/big_{i}.jpg", 'wb') as f:
                f.write(big_res.content)
            time.sleep(1)
    else:
        print(f"获取页面数据异常")


